#include "etopshm.h"

void show_help(void)
{
	printf("cmd:\n");
	printf("\t#etopshm dumpall (dump all item)\n");
	printf("\t#etopshm wipe (wipe all item)\n");
	printf("\t#etopshm set char xxxx 1 (Set xxxx to 1,type=char)\n");
	printf("\t#etopshm get char xxxx (Get xxxx Value,type=char)\n");
	printf("\t#etopshm set int xxxx 1 (Set xxxx to 1,type=int)\n");
	printf("\t#etopshm get int xxxx (Get xxxx Value,type=int)\n");
	printf("\t#etopshm set str xxxx 1 (Set xxxx to 1,type=str)\n");
	printf("\t#etopshm get str xxxx (Get xxxx Value,type=str)\n");
	printf("\t#etopshm -h (Show help info)\n");
	printf("proc (code) : Your main.c must include \"etopshm.h\" first.Call etopshm_init()\n");
	printf("\tetopshm_dumpall() (dump all item)\n");
	printf("\tetopshm_wipe() (wipe all item)\n");
	printf("\tetopshm_set(int type,char *name,void *value)\n");
	printf("\t type : CHAR_TYPE,INT_TYPE,STR_TYPE\n");
	printf("\t name : Your var name\n");
	printf("\t value : Set Value\n");
	printf("\tetopshm_get(int type,char *name,void *value)\n");
	printf("\t type : CHAR_TYPE,INT_TYPE,STR_TYPE\n");
	printf("\t name : Your var name\n");
	printf("\t value : Get Value\n");
}

int main(int argc,char *argv[])
{
	char *item=argv[1];
	char *type=argv[2];
	char *name=argv[3];
	char *setValue=argv[4];
	char cValue;
	int iValue;
	char sValue[ETOPSHM_STR_LEN];
	
	if(item == NULL)
		goto SHOW_HELP;
	
	etopshm_init();
	if(!strcmp(item,"dumpall")){
		etopshm_dumpall();
	}
	else if(!strcmp(item,"wipe")){
		etopshm_wipe();
	}
	else if(!strcmp(item,"set")){
		if(type == NULL || name == NULL)
			goto SHOW_HELP;
		
		if(!strcmp(type,"char")){
			if(setValue == NULL)
				goto SHOW_HELP;
			cValue = atoi(setValue);
			etopshm_set(CHAR_TYPE,name,&cValue);
		}
		else if(!strcmp(type,"int")){
			if(setValue == NULL)
				goto SHOW_HELP;
			iValue = atoi(setValue);
			etopshm_set(INT_TYPE,name,&iValue);
		}
		else if(!strcmp(type,"str")){
			if(setValue == NULL)
				goto SHOW_HELP;
			etopshm_set(STR_TYPE,name,setValue);
		}
	}
	else if(!strcmp(item,"get")){
		if(type == NULL)
			goto SHOW_HELP;
		
		if(!strcmp(type,"char")){
			if(etopshm_get(CHAR_TYPE,name,&cValue)>0)
				printf("%s=%d\n",name,cValue);
		}
		else if(!strcmp(type,"int")){
			if(etopshm_get(INT_TYPE,name,&iValue)>0)
				printf("%s=%d\n",name,iValue);
		}
		else if(!strcmp(type,"str")){
			if(etopshm_get(STR_TYPE,name,sValue)>0)
				printf("%s=%s\n",name,sValue);
		}
	}
	else if(!strcmp(item,"-h")){
		show_help();
	}
	else if(!strcmp(item,"test")){
		char cmd[100];
		int i;
		char ii;
		
		for(i=0,ii=0;i<ETOPSHM_ITEM_CNT;i++,ii++){
			sprintf(cmd,"char%d",i);
			etopshm_set(CHAR_TYPE,cmd,&ii);
			sprintf(cmd,"int%d",i);
			etopshm_set(INT_TYPE,cmd,&i);
			sprintf(cmd,"str%d",i);
			etopshm_set(STR_TYPE,cmd,cmd);
			
			sprintf(cmd,"char%d",i);
			etopshm_get(CHAR_TYPE,cmd,&cValue);
			//printf("%s=%d\n",cmd,cValue);
			sprintf(cmd,"int%d",i);
			etopshm_get(INT_TYPE,cmd,&iValue);
			//printf("%s=%d\n",cmd,iValue);
			sprintf(cmd,"str%d",i);
			etopshm_get(STR_TYPE,cmd,sValue);
			//printf("%s=%s\n",cmd,sValue);
			
			usleep(10);
			if(i==(ETOPSHM_ITEM_CNT-1)){
				etopshm_wipe();
				i=0;
				ii=0;
			}
		}
		
	}
	else
		printf("truedano : Bye,stupid!\n");
	
	return 1;
SHOW_HELP:
	show_help();
	return -1;
}
